package com.cvo.scrumtoolkit.client.pannels;

import java.util.LinkedList;
import java.util.List;

import com.cvo.scrumtoolkit.client.controller.ControllerImpl;
import com.cvo.scrumtoolkit.client.controller.HistoryManager;
import com.cvo.scrumtoolkit.client.controller.Navigator;
import com.cvo.scrumtoolkit.client.controller.View;
import com.cvo.scrumtoolkit.client.entities.BacklogItem;
import com.cvo.scrumtoolkit.client.entities.Project;
import com.cvo.scrumtoolkit.client.entities.Sprint;
import com.cvo.scrumtoolkit.client.entities.WorkItem;
import com.cvo.scrumtoolkit.client.model.ProjectStore;
////import com.cvo.scrumtoolkit.client.pannels.widgets.BacklogInSprintWidget;
import com.cvo.scrumtoolkit.client.pannels.widgets.BacklogInSprintWidget;
import com.cvo.scrumtoolkit.client.pannels.widgets.WorkitemInSprintWidget;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.CheckBox;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.HasText;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.Widget;

public class SprintOverviewPanel extends Composite  {
	
	//private Project project;
	//private LinkedList<Sprint> sprints;
	///private List<BacklogItem> sprintlist;
	private FlexTable vulMetBacklog = new FlexTable();
	String id;
	String sprint;
	private static SprintOverviewPanelUiBinder uiBinder = GWT.create(SprintOverviewPanelUiBinder.class);
	private final PersistentServiceAsync persistentService = GWT.create(PersistentService.class);
	FlowPanel flow= new FlowPanel();
	int r=1;
	
	interface SprintOverviewPanelUiBinder extends UiBinder<Widget, SprintOverviewPanel> {
	}

	
	@UiField
	HorizontalPanel sprintknophorizon;
	@UiField
	HorizontalPanel sprintlijsthorizon;
	

	public SprintOverviewPanel() {
		initWidget(uiBinder.createAndBindUi(this));
			sprintknophorizon.add(flow);	
		try {
			id =Navigator.getNavigator().getProjectId();
			sprint= Navigator.getNavigator().getSprintId();
			System.out.println("Hoe zit het in de sprint?: "+id+"  "+sprint);
			int spr=Integer.parseInt(sprint);
			view(id,spr);
			
		} catch (Exception e1) {
			e1.printStackTrace();
		}
		laadSprints();
		
			}
			
	public void laadSprints(){
		int spr=Integer.parseInt(sprint);
		
		for(int i=1;i<=spr;i++){
		final int vul =i;
		System.out.println("verandert i of verandert i niet?: "+vul);
			Button b =new Button(id+" sprint"+i);
			b.setPixelSize(150, 50);
				flow.add(b);
				b.setStyleName("test");
				b.addClickHandler(new ClickHandler() {
					@Override
					public void onClick(ClickEvent event) {
						sprintlijsthorizon.remove(vulMetBacklog);
						vulMetBacklog.removeAllRows();
						view(id,vul);
						try{
							String	id =Navigator.getNavigator().getProjectId();
							String	sprint= Navigator.getNavigator().getSprintId();
								String string = "sprintbacklog/"+id+"/"+sprint;
								HistoryManager.gotoUrl(string);

							}catch (Exception ex) {
							// TODO: handle exception
							}
					}
				});
					
				}
		
	}
//-------------------------------------------------------------------	
public void view(String id,int sprint){
	System.out.println("in view,de id: "+id+"  "+sprint);
	System.out.println("Is alles weg?:"+vulMetBacklog.getRowCount());
	sprintlijsthorizon.remove(vulMetBacklog);		
	//sprintlist=sprint.getBacklogItems();

	sprintlijsthorizon.add(vulMetBacklog);
	vulMetBacklog.setCellPadding(5);
	
	//vulMetBacklog.setText(0,0,"BacklogItem");
	persistentService.getItemsinSprint(id,sprint, new AsyncCallback<List<BacklogItem>>() {

		
		public void onFailure(Throwable caught) {
			System.out.println("Trouble in Sprintoverview ");
			
		}

		public void onSuccess(List<BacklogItem> result) {
			for(BacklogItem backlogItem:result){
				//final int r =1;
				
					BacklogInSprintWidget bis = new BacklogInSprintWidget(backlogItem);
					vulMetBacklog.setWidget(r, 0, bis);
					persistentService.getWorkItems(backlogItem.getBacklogItemName(), new AsyncCallback<List<WorkItem>>() {

						@Override
						public void onFailure(Throwable caught) {
									
						}

						@Override
						public void onSuccess(List<WorkItem> result) { 
							if(!(result==null)){
								for(WorkItem w: result){
								WorkitemInSprintWidget wis = new WorkitemInSprintWidget(w);	
								vulMetBacklog.setWidget(r, 0, wis);
								 r++;
								}
							}
													
						}
					});
					
					
					
					
					r++;
				

			
			}
				
			
				};
				
	});				
	
	
}	
	
	
	
}	
	
	
	
	
	
	
	
	
	

/* List<WorkItem> wl=backlogItem.getWorkItems();
int i =wl.size();
if (i>0){
 r++;
}
 for(WorkItem w:wl){
	WorkitemInSprintWidget wis = new WorkitemInSprintWidget(w);
	vulMetBacklog.setWidget(r, 0, wis);
	 r++;
 }*/
	
	
		
	/*public void view(Sprint sprint){
		System.out.println(""+sprint.getSprintNummer());
		System.out.println("Is alles weg?:"+vulMetBacklog.getRowCount());
		sprintlijsthorizon.remove(vulMetBacklog);		
		sprintlist=sprint.getBacklogItems();

		sprintlijsthorizon.add(vulMetBacklog);
		vulMetBacklog.setCellPadding(5);
		
		//vulMetBacklog.setText(0,0,"BacklogItem");
		
		
		int r =1;
		
		for(final BacklogItem b: sprintlist){
			BacklogInSprintWidget bis = new BacklogInSprintWidget(b);
			vulMetBacklog.setWidget(r, 0, bis);
				 List<WorkItem> wl=b.getWorkItems();
			 int i =wl.size();
			 if (i>0){
				 r++;
			 }
				 for(WorkItem w:wl){
					WorkitemInSprintWidget wis = new WorkitemInSprintWidget(w);
					vulMetBacklog.setWidget(r, 0, wis);
					 r++;
				 }
			
			r++;
			
	}
			
	
	

	}	
	public void checkForWorkitem(BacklogItem b){
		 List<WorkItem> wl=b.getWorkItems();
		 int i =wl.size();
		 if (i>0){
			 for(WorkItem w:wl){
				 
			 }
		 }
	}
	}			
*/

